using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using JinTai.MemorialTablets;
using Ornament.MVCWebFrame.Models;
using Ornament.Web.Mvc;

namespace Ornament.MVCWebFrame.Controllers
{
    public class ContactsController : Controller
    {
        //
        // GET: /Contacts/
        [JsonContainerFilter]
        public ActionResult Search(JsonContainer data)
        {
            int page = data.ToInt32("page") - 1;
            int pagesize = data.ToInt32("rows");
            int totalNumber;
            IList<Contact> result = MemorialTablesContext.Factory.CreateContactDao().Search(data.ToString("mobile"),
                                                                                            data.ToString("homePhone")
                                                                                            , data.ToString("name"), page, pagesize,
                                                                                            out totalNumber);
            var list = new ArrayList();

            foreach (Contact s in result)
            {
                list.Add(new { id = s.Id, cell = new[] { s.Name, s.Mobile, s.HomePhone, s.Address.ToString() } });
            }

            var dataJson = new
                               {
                                   total = (totalNumber / pagesize) + 1,
                                   page = page + 1,
                                   records = totalNumber,
                                   rows = list
                               };
            return Json(dataJson);
        }

        public ActionResult GetContacts(string[] ids)
        {
            var idset = new Guid[ids.Length];
            int i = 0;
            foreach (string id in ids)
            {
                idset[i] = new Guid(id);
                i++;
            }
            return Json(MemorialTablesContext.Factory.CreateContactDao().GetContacts(idset));
        }

        public ActionResult PopupSearch()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Save(FormCollection data)
        {
            Contact contact = data["id"] == "" ? new Contact() : MemorialTablesContext.Factory.CreateContactDao().Get(new Guid(data["id"]));
            contact.Name = data["name"];
            contact.HomePhone = String.IsNullOrEmpty(data["prefixNumber"])
                              ? ""
                              : data["prefixNumber"] + "-" + data["HomePhone"];
            contact.Mobile = data["Mobile"];
            contact.Memo = data["Memo"];
            contact.Address = new Addressbook { Address = data["Address"], City = data["City"], District = data["District"] };
            MemorialTablesContext.Factory.CreateContactDao().SaveOrUpdate(contact);
            return RedirectToAction("Index", new RouteValueDictionary { { "editId", contact.Id } });
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(string id)
        {
            ViewData["tablet"] = MemorialTablesContext.Factory.CreateMemorialTabletDao().FindByContact(new Guid(id));
            return View(MemorialTablesContext.Factory.CreateContactDao().Get(new Guid(id)));
        }
    }
}